import React, { useEffect, useState } from 'react'
import { View, Button, Image, Text } from '@tarojs/components'
import Taro, { useDidShow } from '@tarojs/taro'
import { fetchWechatAuth, fetchXchPhone, fakeStudentWithWechat } from "@/server/login";
import { wechatAuth, toast, loginData, goBack } from "@/utils/utils";
import Router from '@/utils/router'
import './index.scss'
const LoginSelect = (props) => {
    const [check, setCheck] = useState(false)
    const [data, setData] = useState({
        mobile: '',
    });
    useDidShow(() => {
        const data = Taro.getStorageSync("userInfo") || {};
        const { organizationId } = data;
        wechatAuth((e) => {
            fetchWechatAuth({ code: e, organizationId: organizationId }).then((val) => {
                const { userInfo = {} } = val
                const { unionId, openId, token } = userInfo
                if (token) {
                    Taro.setStorageSync('userInfo', { ...loginData() || {}, ...userInfo });
                    goBack()
                    return
                }
                setData({
                    ...data,
                    unionId,
                    openId
                })

            });
        });
    })
    const fetchMobile = (e) => {
        const { detail = {} } = e
        const { errMsg, code } = detail
        const useData = Taro.getStorageSync("userInfo") || {};
        if (errMsg === 'getPhoneNumber:ok') {
            fetchXchPhone({
                code
            }).then(val => {
                const { phone } = val
                const payload = {
                    ...useData,
                    ...data,
                    mobile: phone,
                    mobileType: 'wechatQuick',
                    source: 'miniProgram',
                }
                fakeStudentWithWechat(payload).then(e => {
                    const { userInfo } = e;
                    Taro.setStorageSync('userInfo', { ...useData, ...data, ...userInfo });
                    goBack()
                })
            })
        }
        else {
            toast('获取失败')
        }
    }
    return <View className='LoginSelect_box'>
        <View className='wechatLogin_body_box'>
            <Image className='wechatLogin_body_img' src={'https://gygj-wechat.oss-cn-hangzhou.aliyuncs.com/wechatImage/loginQt.png'}></Image>
            <View style={{ position: 'relative' }} onClick={() => {
                if (!check) {
                    toast('请阅读并同意隐私协议和服务协议后登录')
                }
            }} className=''>
                {check && <Button
                    openType='getRealtimePhoneNumber'
                    onGetRealTimePhoneNumber={fetchMobile}
                    style={{
                        position: 'absolute',
                        top: 0,
                        left: 0,
                        right: 0,
                        bottom: 0,
                        zIndex: 10,
                        opacity: 0
                    }}></Button>}
                <View className='wechatLogin_body_imgLogin' >
                    一键登录
                </View>
            </View>

            <View onClick={() => {
                if (!check) {
                    toast('请阅读并同意隐私协议和服务协议后登录')
                } else {
                    Router('loginCode', {
                        linkType: "redirectTo"
                    })
                }
            }} className='wechatLogin_body_btnCode'>
                手机验证码注册
            </View>
            <View className='wechatLogin_rule wechatLogin_rule_color1'>
                <View onClick={() => setCheck(!check)} className={check ? 'wechatLogin_radioSelect' : 'wechatLogin_radio_icon'}></View>   我已阅读并同意
                <Text onClick={() => {
                    Router('webView', {
                        args: {
                            src: 'https://wx.gy.cn/rule/login.html'
                        }
                    })
                }} className='wechatLogin_rule_color2'>《学管家服务协议》</Text>
                和
                <Text onClick={() => {
                    Router('webView', {
                        args: {
                            src: 'https://wx.gy.cn/rule/ys.html'
                        }
                    })
                }} className='wechatLogin_rule_color2'> 《隐私政策》</Text>
            </View>
        </View>
    </View>

}
export default LoginSelect